// pages/manage/manage.js
var verify = require('../../../../utils/verify-token.js')
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    size: 10,
    loading: true,
    filterValue: "",
    isFresh: false,
    inputShowed: false,
    chose: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sh: app.globalData.screenHeight,
      sw: app.globalData.screenWidth,
      commodityPriceId: options.commodityPriceId,
      productId: options.productId
    })
    this.getChoseList();
  },

  // 获取已选设备列表
  getChoseList() {
    var commodityPriceId = this.data.commodityPriceId
    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this;		
    wx.request({
      url: baseUrl + 'commodity_price/'+commodityPriceId+'/devices' ,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var choseList = res.data.data
          that.setData({
            choseList: choseList.commodityPriceDevices
          })
          that.getDeviceList();
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },

  // 获取设备列表
  getDeviceList() {
    var choseList = this.data.choseList
    var productId = this.data.productId
    var companyId = app.globalData.companyId
    var size = this.data.size
    var token = app.globalData.token
    var filterValue = this.data.filterValue
    var that = this
    wx.request({
      url: 'https://web.api.sdaascloud.com/web/searchDeviceInCommdityPrice?productId=' + productId + '&companyId=' + companyId + '&locationId=&identifier=' + filterValue+'&page=0&size=' + size,
      header: {
        'AUTHORIZATION': token
      },
      success: function (res) {
        if (res.data.code == 60006) {
          verify.clearAll()
        } else if (res.data.code == 200) {
          var defaultList = res.data.data.content
          // console.log("defaultList",defaultList)
          // console.log("choseList = ", choseList)

          var totalElements = res.data.data.totalElements
          var loading = false
          if (totalElements > size) {
            loading = true
          }
          var i;
          var j;
          for (i = 0; i < defaultList.length; i++) {
            for (j = 0; j < choseList.length; j++) {
              if (defaultList[i].identifier == choseList[j].identifier) {
                // defaultList.splice(i,1);
                defaultList[i].remark = true
                continue;
              }
            }
          }
          // console.log("deviceList = ", defaultList)
          that.setData({
            deviceList: defaultList,
            totalElements: totalElements,
            loading: loading,
            isFresh: false,
            chose: false
          })
        } else {
          wx.showModal({
            title: '操作失败',
            content: res.data.message,
          })
        }
      }
    })
  },
  inputTyping: function (e) {
    
    this.setData({
      filterValue: e.detail.value
    });
    this.search();
  },
  // // 搜索框输入
  // input: function (res) {
  //   this.setData({
  //     filterValue: res.detail.value
  //   })
  // },

  search: function () {
    var filterValue = this.data.filterValue
    var size = 10
    this.setData({
      filterValue: filterValue,
      size: size
    })
    this.getDeviceList()
  },

  // 转换
  transfer() {
    var choseList = this.data.choseList
    this.setData({
      deviceList: choseList,
      chose: true,
      filterValue: "",
      size: 10
    })
  },

  // 进行绑定设备
  bind(res) {
    var deviceList = this.data.deviceList
    var index = res.currentTarget.dataset.index
    var device = this.data.deviceList[index]
		// console.log(device);
    var commodityPriceId = this.data.commodityPriceId
    var commodityPriceDevices = [{ "deviceId": device.deviceId, "deviceIdentifier": device.identifier, "locationId":"" }]
    var chose = this.data.chose
//     console.log("commodityPriceDevices = ",commodityPriceDevices)
//     console.log("commodityPriceId = ", commodityPriceId)

    var token = app.globalData.token
    var baseUrl = app.globalData.baseUrl
    var that = this

    if (!chose) {
      wx.request({
        url: baseUrl + 'commodity_price/' + commodityPriceId + '/devices',
        method: 'put',
        data: {
          "commodityPriceDevices": commodityPriceDevices
        },
        header: {
          'AUTHORIZATION': token
        },
        success: function (res) {
          // console.log("res = ", res)
          if (res.data.code == 60006) {
            verify.clearAll()
          } else if (res.data.code == 200) {
            wx.showToast({
              title: '绑定成功!',
              duration: 3500
            })
            that.getChoseList();
          } else {
            wx.showModal({
              title: '操作失败',
              content: res.data.message,
            })
          }
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var chose = this.data.chose
    if (!chose) {
      var size = this.data.size * 1 + 10
      var loading = this.data.loading
      var totalElements = this.data.totalElements
      this.setData({
        isFresh: true
      })
      if (size - 10 > totalElements) {
        this.setData({
          loading: false
        })
      } else {
        this.setData({
          size: size
        })
        this.getDeviceList()
      }
    }
  }
})